﻿<?php
use Classes\autoload\SplClassLoader;
use Classes\db\DBConnection;
use Classes\Login;
use Classes\UserPrincipal;
use Classes\User;
use Classes\Post;
use Classes\Event;
use html\RenderHTML;
use html\Follow;
use html\SearchResults;
use html\ProfileView;
use html\EventView;
use Classes\Relation;

require 'Classes/autoload/SplClassLoader.php';
$loader = new SplClassLoader ();
$loader->register ();

session_start ();

if (! isset ( $_SESSION ['userPrincipal'] )) {
	header ( "Location: index.php" );
	exit ();
} else {
	$userPrincipal = $_SESSION ['userPrincipal'];
}

html\RenderHTML::renderHeader ( 'Начало' );
html\RenderHTML::addStyle ( 'styles/global.css' );
html\RenderHTML::addStyle ( 'styles/index.css' );
html\RenderHTML::renderBody ();

echo '<div id="menu">';

echo '<ul>';

echo '<li><a href="home.php">Начало</a></li>';
echo '<li><a href="?profile=' . $userPrincipal->getUserId () . '">За мен</a></li>';
echo '<li><a href="?search=1">Намери потребител</a></li>';
echo '<li id="events-li">Събития
			<ul id="events-ul">
				<li><a href="?search=2&host=' . $userPrincipal->getUserId () . '">Моите събития</a></li>
				<li><a href="?search=2">Намери събитие</a></li>
				<li><a href="?event-cr=yes">Създай събитие</a></li>	
			</ul>
	  </li>';

echo '<li id="follow">Следване
			<ul>
				<li><a href="?follow=1">Хора, които следвам</a></li>
				<li><a href="?follow=2">Хора, които ме следват</a></li>
			</ul>
	  </li>';

echo '<li><a href="logout.php">Изход</a></li>';

if ($userPrincipal->getRank () == 10) {
	echo '<li><a href="admin/index.php">Администраторски панел </a></li>';
}

echo '</ul>';

echo '</div>';
// //////////////////////////////////////////////////////////////////////////////////////////////////////////

echo '<div id="center">';

echo '<form action="home.php" method="post" id="post-form">
	  	<fieldeset>
			<input type="hidden" name="post" value="yes"/>
			<textarea id="post-area" name="post-area"></textarea>
			<input type="POST-submit" value="Сподели"/>
		</fieldset>
	  </form>';

if (isset ( $_POST ['post'] ) && $_POST ['post'] == 'yes') {
	$content = strip_tags ( $_POST ['post-area'] );
	$postDate = date ( 'Y-m-d H-i-s' );
	Post::insertPost ( $userPrincipal->getUserId (), $content, $postDate );
}

if (isset ( $_GET ['deletepost'] ) && $_GET ['deletepost'] == "yes" && isset ( $_GET ['post_id'] )) {
	Post::deletePost ( $_GET ['post_id'] );
}

if (isset ( $_GET ['profile'] )) {
	html\ProfileView::showPosts ( $_GET ['profile'] );
} else {
	ProfileView::showPosts ( $userPrincipal->getUserId () );
}

echo '</div>';

echo '<div id="right">';

if (isset ( $_GET ['follow'] )) {
	if ($_GET ['follow'] == 1) {
		html\Follow::showFollowings ( $userPrincipal->getUserId () );
	}
	
	if ($_GET ['follow'] == 2) {
		html\Follow::showFollowers ( $userPrincipal->getUserId () );
	}
}

if (isset ( $_GET ['relation'] ) && $_GET ['relation'] == 'end' && isset ( $_GET ['user'] )) {
	Relation::deleteRelation ( $userPrincipal->getUserId (), $_GET ['user'] );
	header ( "Location: ?follow=1" );
	exit ();
}

if (isset ( $_GET ['relation'] ) && $_GET ['relation'] == 'start' && isset ( $_GET ['user'] )) {
	Relation::insertRelation ( $userPrincipal->getUserId (), $_GET ['user'] );
	header ( "Location: ?follow=1" );
	exit ();
}

if (isset ( $_GET ['search'] ) && $_GET ['search'] == 1) {
	if (isset ( $_GET ['user-name'] )) {
		html\SearchResults::showUsers ( $_GET ['user-name'] );
	} else {
		
		echo '<form action="home.php" id="search-user-form">
				<fieldset>
					<input type="hidden" name="search" value="1"/>
					<input type="text" name="user-name"/>
					<input type="submit" value="Търси" id="submit-user-name"/>
				</fieldset>
			</form>';
	}
}

if (isset ( $_GET ['search'] ) && $_GET ['search'] == 2) {
	if (isset ( $_GET ['host'] )) {
		html\SearchResults::showMyEvents ( $userPrincipal->getUserId () );
	} else {
		if (isset ( $_GET ['input-2'] ) && $_GET ['input-2'] != "") {
			html\SearchResults::showEvents ( $_GET ['input-2'] );
		} else {
			echo '<form action="home.php" id="search-event-form">
				  	<input type="hidden" name="search" value="2"/>
			 		<input type="text" name="input-2"/>
					<input type="submit" value="Търси" id="submit-event-name"/>
			</form>';
		}
	}
}

if (isset ( $_GET ['event'] )) {
	html\EventView::showEvent ( $_GET ['event'] );
}

if (isset ( $_GET ['event-cr'] ) && $_GET ['event-cr'] == 'yes') {
	echo '<form action="home.php" method="POST" name="create-event-form">
			<fieldset>
				<legend>Създаване на събитие</legend>
				<input type="hidden" name="cr" value="yes"/>
		  		<div>
					<label for="event-name">Име:</label>
					<input type="text" id="event-name" name="event-name"/>
				</div>
				<div>
					<label for="description">Описание:</label>
					<input type="text" id="description" name="description"/>
				</div>
				<div>
					<label for="place">Място:</label>
					<input type="text" id="place" name="place"/>
				</div>
				<div>
					<label for="date">Начало:</label>
					<input type="text" id="date" name="date" placeholder="YYYY-MM-DD HH-MM-SS"/>
				</div>
				<div>
					<input type="submit" value="Създай" id="submit-create-event"/>
				</div>
			<fieldset>
		  </form>';
}

if (isset ( $_POST ['cr'] ) && $_POST ['cr'] == "yes") {
	$author = $userPrincipal->getUserId ();
	$name = strip_tags ( $_POST ['event-name'] );
	$date = strip_tags ( $_POST ['date'] );
	$description = strip_tags ( $_POST ['description'] );
	$place = strip_tags ( $_POST ['place'] );
	Classes\Event::insertEvent ( $name, $date, $author, $description, $place );
	
	html\SearchResults::showMyEvents ( $userPrincipal->getUserId () );
}

if (isset ( $_GET ['del-event'] )) {
	Classes\Event::deleteEvent ( $_GET ['del-event'] );
	html\SearchResults::showEvents ( $userPrincipal->getUserId () );
}
// ////////////////////////////////////////////////////////
if (isset ( $_GET ['profile'] )) {
	html\ProfileView::showUserInfo ( $_GET ['profile'] );
	if (Relation::isRelation ( $userPrincipal->getUserId (), $_GET ['profile'] ) == 1) {
		echo '<a href="?relation=end&user=' . $_GET ['profile'] . '">Спри да следваш</a>';
	}
	if (Relation::isRelation ( $userPrincipal->getUserId (), $_GET ['profile'] ) == 0 && $_GET ['profile'] != $userPrincipal->getUserId ()) {
		echo '<a href="?relation=start&user=' . $_GET ['profile'] . '">Следвай</a>';
	}
}

echo '</div>';

html\RenderHTML::renderFooter ();

